package com.rachel.studyapp.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public abstract class RBaseAdapter<T> extends RecyclerView.Adapter<RBaseViewholder> {
    private int mLayoutId;
    private List<T> mDatas; //取泛型是因为不知道bean是啥类型的。
    public RBaseAdapter(int layoutid,List<T> list) {
        mLayoutId = layoutid;
        mDatas = list;
    }
    @Override
    public RBaseViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        RBaseViewholder viewholder = RBaseViewholder.getViewHolder(parent.getContext(),
                mLayoutId,parent);
        return viewholder;
    }
    @Override
    public void onBindViewHolder(RBaseViewholder holder, final int position) {
        holder.getConserView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            }
        });
        getConver(holder,mDatas.get(position)); //提供 viewholder 出去，数据由用户处理
    }
    public abstract void getConver(RBaseViewholder holder,T data);
    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 添加点击事件
     */
    public interface  onItemClickListener{
        void onItemClick(View view, int position);
    }
    private onItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(onItemClickListener listener){
        mOnItemClickListener = listener;
    }
}